package com.laf.mall.api.controller;

import com.laf.mall.api.dto.Mall;
import com.laf.mall.api.dto.MemberCar;
import com.laf.mall.api.dto.MemberSetting;
import com.laf.mall.api.dto.PointsDeduction;
import com.laf.mall.api.service.MemberCarService;
import com.laf.mall.api.service.MemberSettingService;
import com.laf.mall.api.service.PointsDeductionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @author ygl
 * @create 2018-12-26
 * @DESCRIPTION
 **/
@RestController
@RequestMapping("/member/car")
public class MemberCarController {

    @Autowired
    private MemberCarService memberCarService;

    @Autowired
    private MemberSettingService memberSettingService;

    @Autowired
    private PointsDeductionService pointsDeductionService;


    /**
     * 通过会员ID查询绑定的车辆信息
     * @param memberId
     * @return
     */
    @GetMapping("/{memberId}")
    private ResponseEntity findByMemberId(@PathVariable Integer memberId){
        List<MemberCar> list = memberCarService.findByMemberId(memberId);
        return new ResponseEntity(list, HttpStatus.OK);
    }

    /**
     * 绑定车辆
     * @param model
     * @return
     */
    @PostMapping("/bind")
    private ResponseEntity bind(@RequestBody MemberCar model){
        /**
         * 绑定之前查询这个车辆是否已经被绑定了
         * 如果已经绑定了 返回-1表示车辆已经被绑定了
         */
        List<MemberCar> carList = memberCarService.findByCarNumber(model.getCarNumber());
        if (!CollectionUtils.isEmpty(carList)) {
            return new ResponseEntity("-1", HttpStatus.OK);
        }
        int insert = memberCarService.insert(model.getMemberId(), model.getCarNumber());
        /**
         * 绑定成功之后，查看积分抵扣停车费的设置，如果没有那就需要新增，如果有就不操作
         */
        if(insert > 0 ){
            List<MemberSetting> memberSettingList = memberSettingService.findByMemberId(model.getMemberId());
            if (CollectionUtils.isEmpty(memberSettingList)) {
                memberSettingService.insert(model.getMemberId(), 1);
            }
        }
        return new ResponseEntity(insert, HttpStatus.OK);
    }

    /**
     * 解绑车辆
     * @param id
     * @return
     */
    @GetMapping("/unbind/{id}")
    private ResponseEntity unbind(@PathVariable Integer id){
        int insert = memberCarService.deleteById(id);
        return new ResponseEntity(insert, HttpStatus.OK);
    }


    @GetMapping("/setting/{memberId}")
    private ResponseEntity findSetting(@PathVariable Integer memberId){
        List<MemberSetting> memberSettings = memberSettingService.findByMemberId(memberId);
        if (CollectionUtils.isEmpty(memberSettings)) {
            return new ResponseEntity(null, HttpStatus.OK);
        } else {
            return new ResponseEntity(memberSettings.get(0), HttpStatus.OK);
        }
    }

    @PostMapping("/setting")
    private ResponseEntity updateSetting(@RequestBody MemberSetting memberSetting){
        List<MemberSetting> memberSettingList = memberSettingService.findByMemberId(memberSetting.getMemberId());
        if (CollectionUtils.isEmpty(memberSettingList)) {
            int insert = memberSettingService.insert(memberSetting.getMemberId(), memberSetting.getAutoOffsetParkingRate());
            return new ResponseEntity(insert, HttpStatus.OK);
        }
        int update = memberSettingService.update(memberSetting.getMemberId(), memberSetting.getAutoOffsetParkingRate());
         return new ResponseEntity(update, HttpStatus.OK);
    }

    /**
     * 积分抵扣记录
     * @param memberId
     * @return
     */
    @GetMapping("/deduction/{memberId}")
    private ResponseEntity findPointsDeduction(@PathVariable Integer memberId){
        List<PointsDeduction> pointsDeductionList = pointsDeductionService.findByMemberId(memberId);
        if (CollectionUtils.isEmpty(pointsDeductionList)) {
            return new ResponseEntity(null, HttpStatus.OK);
        } else {
            return new ResponseEntity(pointsDeductionList, HttpStatus.OK);
        }
    }
}
